#include "homepage.h"
#include "ui_homepage.h"
#include <QDebug>
#include <QKeyEvent>

HomePage * HomePage::hp = nullptr;

HomePage::HomePage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HomePage)
{
    ui->setupUi(this);
    this->setWindowTitle("计算器");      //标题
    //this->setWindowIcon(QIcon(""));     //图标
    //改变尺寸
    this->resize(400, 600);             //改变尺寸
    //this->setFixedSize(400, 600);       //设置固定尺寸
    connect(ui->btnNumber0,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber1,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber2,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber3,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber4,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber5,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber6,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber7,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber8,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnNumber9,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
    connect(ui->btnPoint,
            &QPushButton::clicked,
            this,
            &HomePage::btnNumberClickedSlot);
}

HomePage::~HomePage()
{
    delete ui;
}

HomePage *HomePage::getObject()
{
    if(hp == nullptr)
        hp = new HomePage;
    return hp;
}

void HomePage::myMath()
{
    double x,y;
    x = ui->labSum->text().toDouble();
    y = ui->labOldNum->text().toDouble();
    if(x == 0.0)
        return;
    switch (mathType) {
    case -1:    //=
        ui->labOldNum->setText(ui->labSum->text());
        break;
    case 0:     // +
        ui->labOldNum->setNum(x + y);
        break;
    case 1:     // -
        ui->labOldNum->setNum(y - x);
        break;
    case 2:     // *
        ui->labOldNum->setNum(x * y);
        break;
    case 3:     // /
        ui->labOldNum->setNum(y / x);
        break;
    }
    ui->labSum->clear();
    pointNum = 0;
}

void HomePage::dispNumber(int i)
{
    //-1 ~ 9   -1 --> .
    //小数点数量判断
    if(i == -1)
    {
        if(pointNum != 0)
            return;
        pointNum = 1;
    }
    //数字判断
    if(i < -1 || i > 9)
    {
        return;
    }
    QString oldText = ui->labSum->text();
    if(oldText == "0")
        oldText.clear();
    if(i == -1 && oldText == "") //显示小数点
    {
        oldText = "0";
    }
    QString newText = oldText +
            (i == -1 ?
                 QString(".") :
                 QString("%1").arg(i));
    ui->labSum->setText(newText);
}

void HomePage::keyPressEvent(QKeyEvent *event)
{
    int key = event->key();
    if(key >= 0x30 && key <= 0x39)
    {
        //数字按下
        dispNumber(key - 0x30);
    }
    if(key == Qt::Key_Period)
    {
        //小数点按下
        dispNumber(-1);
    }
    if(key == Qt::Key_Plus)
    {// +
        on_btnAdd_clicked();
    }
    if(key == Qt::Key_Minus)
    {// -
        on_btnSub_clicked();
    }
    if(key == Qt::Key_Asterisk)
    {// *
        on_btnMul_clicked();
    }
    if(key == Qt::Key_Slash)
    {// /
        on_btnDiv_clicked();
    }
    if(key == Qt::Key_Equal || key == Qt::Key_Return)
    {// =
        on_btnSum_clicked();
    }
}

void HomePage::btnNumberClickedSlot()
{
    //0 ~ 9
    //获取按下的按键
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    QString btnKey = btn->text();
    if(btnKey == "·")
    {
        dispNumber(-1);
        return;
    }
    dispNumber(btnKey.toInt());
}
/*
void HomePage::on_btnNumber0_clicked()
{
    //数字0
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber1_clicked()
{
    //数字1
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 1;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber2_clicked()
{
    //数字2
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 2;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber3_clicked()
{
    //数字3
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 3;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber4_clicked()
{
    //数字4
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 4;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber5_clicked()
{
    //数字5
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 5;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber6_clicked()
{
    //数字6
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 6;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber7_clicked()
{
    //数字7
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 < oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 7;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber8_clicked()
{
    //数字8
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 <= oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 8;
    //显示新数字
    ui->labSum->setNum(newNumber);
}

void HomePage::on_btnNumber9_clicked()
{
    //数字9
    //获取旧数字
    int oldNumber = ui->labSum->text().toInt();
    //判断旧数字余量
    if(214748364 <= oldNumber)
        return;
    //计算新数字
    int newNumber = oldNumber * 10 + 9;
    //显示新数字
    ui->labSum->setNum(newNumber);
}
*/
void HomePage::on_btnAdd_clicked()
{
    myMath();       //运算
    mathType = 0;   //记录新的运算符号
}

void HomePage::on_btnSub_clicked()
{
    myMath();       //运算
    mathType = 1;   //记录新的运算符号
}

void HomePage::on_btnMul_clicked()
{
    myMath();       //运算
    mathType = 2;   //记录新的运算符号
}

void HomePage::on_btnDiv_clicked()
{
    myMath();       //运算
    mathType = 3;   //记录新的运算符号
}

void HomePage::on_btnSum_clicked()
{
    myMath();
    mathType = -1;
}
